package lib.player;

import java.awt.Color;
import java.awt.Graphics2D;

import lib.Config;
import lib.grav.ForceHandler;
import lib.player.items.Item;
import server.TCPServerUnit;

public class Player {

	public String name;
	public int nr;
	public int Team;
	public Color color;
	public int life = 100;
	public int points = 0;

	public double xPos;
	public  double yPos;
	private Config cfg = Config.getInstance();

	// Variables needed for animations
	public double oldXpos = 0;
	public double oldYpos = 0;
	public double direction = 0;
	public ForceHandler forceHandler = null; // only used by server side.
	public TCPServerUnit unit = null;
	public Item ITEM = new Item();
	public int TeamPoits = 0;
	public String DisplayName ;

	/**
	 * 
	 * @param xPos
	 *            The Player position on the X axis
	 * @param yPos
	 *            The Player position on the Y axis
	 * @param nr
	 *            The NR. of the Player
	 * @param team
	 *            The NR. of the Team
	 * @param name
	 *            The Name of the Player
	 */

	public Player(int xPos, int yPos, int nr, int team, String name) {
		this.xPos = xPos;
		this.yPos = yPos;
		oldXpos = xPos;
		oldYpos = yPos;
		this.nr = nr;
		this.Team = team;
		this.name = name;
		DisplayName=name;
		if (name.length() >= 10)
			DisplayName = name.substring(0, 10) + "... ";

		switch (team) {
		case 1:
			color = Color.red;
			break;
		case 2:
			color = Color.blue;
			break;
		default:
			color = null;
		}
	}

	/**
	 * 
	 * @param xPos
	 *            X Position of the Player
	 * @param yPos
	 *            Y Position of the Player
	 */
	public void setPos(int xPos, int yPos) {
		this.xPos = xPos;
		this.yPos = yPos;

	}

	public boolean updateDirection() {
		boolean moved = false;
		direction = getDrection();
		if (oldXpos != xPos) {
			oldXpos = xPos;
			moved = true;
		}
		if (oldYpos != yPos) {
			oldYpos = yPos;
			moved = true;
		}
		return moved;
	}

	public int getDrection() {
		double x = (oldXpos - xPos);
		double y = (oldYpos - yPos);
		if (y > 0 && x == 0)
			return 0;
		if (x < 0 && y < 0)
			return 1;
		if (x < 0 && y == 0)
			return 2;
		if (x < 0 && y < 0)
			return 3;
		if (y < 0 && x == 0)
			return 4;
		if (x > 0 && y < 0)
			return 5;
		if (x > 0 && y == 0)
			return 6;
		if (x > 0 && y > 0)
			return 7;
		return 0;
	}

	public String toString() {
		return (int)xPos + " " + (int)yPos + " " + nr + " " + Team + " " + name;
	}

	public void drawItem(Graphics2D g, int pahse) {
		g.setColor(Config.fontColor);
		g.drawString("Your Item:" + ITEM.Name, cfg.xRes / 2 - 60, cfg.yRes
				- cfg.yRes / 30 * 6 + 20);
		g.setColor(color);
		g.fillRoundRect(cfg.xRes / 2 - 50, cfg.yRes - cfg.yRes / 30 * 6 + 30,
				100, 100, 20, 20);
		g.drawImage(ITEM.getTexture(pahse), cfg.xRes / 2 - 50, cfg.yRes
				- cfg.yRes / 30 * 6 + 30, null);
	}
}
